package com.todod.base.module.base.controller.admin.rtmaintain.rtmaintainchild;

import com.alibaba.fastjson.JSON;
import com.todod.base.framework.common.pojo.MyCommonResult;
import com.todod.base.module.base.controller.admin.rtmaintain.rtmaintainchild.vo.RtMaintainChildPageReqVO;
import com.todod.base.module.base.controller.admin.rtmaintain.rtmaintainchild.vo.RtMaintainChildRespVO;
import com.todod.base.module.base.controller.admin.rtmaintain.rtmaintainchild.vo.RtMaintainChildSaveReqVO;
import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;

import jakarta.validation.*;

import com.todod.base.framework.common.pojo.CommonResult;
import com.todod.base.framework.common.util.object.BeanUtils;
import static com.todod.base.framework.common.pojo.CommonResult.success;

import com.todod.base.module.base.dal.dataobject.rtmaintain.rtmaintainchild.RtMaintainChildDO;
import com.todod.base.module.base.service.rtmaintain.rtmaintainchild.RtMaintainChildService;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Tag(name = "管理后台 - 实时数据维护子表")
@RestController
@RequestMapping("/base/rt-maintain-child")
@Validated
public class RtMaintainChildController {

    @Resource
    private RtMaintainChildService rtMaintainChildService;



    @PutMapping("/update")
    @Operation(summary = "更新子表信息")
    public CommonResult<Boolean> updateRtMaintainChild(@Valid @RequestBody RtMaintainChildSaveReqVO updateReqVO) {
        rtMaintainChildService.updateRtMaintainChild(updateReqVO);
        return success(true);
    }

    @GetMapping("/getbyparentid")
    @Operation(summary = "根据父级ID获取对应子表信息")
    public CommonResult<RtMaintainChildRespVO> getRtMaintainChildByParentId(@RequestParam("parentId") Long parentId) {
        RtMaintainChildPageReqVO reqVO = new RtMaintainChildPageReqVO();
        RtMaintainChildDO rtMaintainChildDO = rtMaintainChildService.getRtMaintainChildByParentId(reqVO.setParentId(parentId));
        return success(BeanUtils.toBean(rtMaintainChildDO, RtMaintainChildRespVO.class));
    }

    //------------------------------------------------------------------------------------------------------------------------------

    @RequestMapping("/getnewone")
    @Operation(summary = "按照要求取一条最新数据（通用）")
    public CommonResult<RtMaintainChildRespVO> getRtMaintainChildNewOne(@Valid RtMaintainChildPageReqVO reqVO) {
        RtMaintainChildDO rtMaintainChild = rtMaintainChildService.getRtMaintainChildNewOne( reqVO );
        return success(BeanUtils.toBean(rtMaintainChild, RtMaintainChildRespVO.class));
    }
    @RequestMapping("/getrtsecondbytime")
    @Operation(summary = "实时秒数据:根据时间要求查询实时秒数据(列表)")
    public CommonResult<List<RtMaintainChildRespVO>> getRTSecondByTimeList( @Valid RtMaintainChildPageReqVO reqVO ) {
        List<RtMaintainChildDO>  rtMaintainChildDOList = rtMaintainChildService.getRtMaintainChildList( reqVO );
        return success(BeanUtils.toBean(rtMaintainChildDOList, RtMaintainChildRespVO.class));
    }

    @RequestMapping("/getmwmeterbywelldepth")
    @Operation(summary = "实测整米数据:根据井深范围要求查询实测整米数据(列表)")
    public CommonResult<List<RtMaintainChildRespVO>> getMWMeterByWellDepth( @Valid RtMaintainChildPageReqVO reqVO ) {
        List<RtMaintainChildDO>  rtMaintainChildDOList = rtMaintainChildService.getRtMaintainChildList( reqVO );
        return success(BeanUtils.toBean(rtMaintainChildDOList, RtMaintainChildRespVO.class));
    }

    @RequestMapping("/getlwmeterbywelldepth")
    @Operation(summary = "迟到整米数据:根据迟到井深范围要求查询实测整米数据(列表)")
    public CommonResult<List<RtMaintainChildRespVO>> getLWMeterByWellDepth( @Valid RtMaintainChildPageReqVO reqVO ) {
        List<RtMaintainChildDO>  rtMaintainChildDOList = rtMaintainChildService.getRtMaintainChildList( reqVO );
        return success(BeanUtils.toBean(rtMaintainChildDOList, RtMaintainChildRespVO.class));
    }

    @RequestMapping("/getmidepthbywelldepth")
    @Operation(summary = "实测间隔深度数据:根据井深范围要求查询实测间隔深度数据(列表)")
    public CommonResult<List<RtMaintainChildRespVO>> getmidepthByWellDepth( @Valid RtMaintainChildPageReqVO reqVO ) {
        List<RtMaintainChildDO>  rtMaintainChildDOList = rtMaintainChildService.getRtMaintainChildList( reqVO );
        return success(BeanUtils.toBean(rtMaintainChildDOList, RtMaintainChildRespVO.class));
    }

    @RequestMapping("/getlidepthbywelldepth")
    @Operation(summary = "迟到间隔深度数据:根据迟到井深范围要求查询实测整米数据(列表)")
    public CommonResult<List<RtMaintainChildRespVO>> getlidepthByWellDepth( @Valid RtMaintainChildPageReqVO reqVO ) {
        List<RtMaintainChildDO>  rtMaintainChildDOList = rtMaintainChildService.getRtMaintainChildList( reqVO );
        return success(BeanUtils.toBean(rtMaintainChildDOList, RtMaintainChildRespVO.class));
    }

    //------------------------------------------------------------------------------------------------------------------------------
    @RequestMapping("/getchildbyid")
    @Operation(summary = "取某一条明细数据---根据id获取其明细数据（子表单表查询，一条）")
    public CommonResult<RtMaintainChildRespVO> getRtMaintainChildByCode(@RequestParam("id")  Long id ) {
        RtMaintainChildDO rtMaintainChild = rtMaintainChildService.getRtMaintainChildById(id);
        return success(BeanUtils.toBean(rtMaintainChild, RtMaintainChildRespVO.class));
    }
    @RequestMapping("/getchildbycode")
    @Operation(summary = "取仪器的最新一条数据---根据仪器编号获取其最新的传输过来的明细数据（子表单表查询，一条）")
    public CommonResult<RtMaintainChildRespVO> getRtMaintainChildByCode(@RequestParam("code")  String code ) {
        RtMaintainChildDO rtMaintainChild = rtMaintainChildService.getRtMaintainChildByCode( code );
        return success(BeanUtils.toBean(rtMaintainChild, RtMaintainChildRespVO.class));
    }
    @RequestMapping("/getchildbycreatetime")
    @Operation(summary = "取仪器的一天内的时间范围内数据---根据仪器代码和时间段参数获取其最新的传输过来的列表数据（子表单表查询，列表，时间正序）")
    public CommonResult<List<RtMaintainChildRespVO>> getRtMaintainChildByCreateTime( @Valid RtMaintainChildPageReqVO reqVO ) {
        List<RtMaintainChildDO>  rtMaintainChildDOList = rtMaintainChildService.getRtMaintainChildByCreateTimeList( reqVO );
        return success(BeanUtils.toBean(rtMaintainChildDOList, RtMaintainChildRespVO.class));
    }
    //---------------------------------------------------------------------------------------------------------------------------------------------
    @RequestMapping("/getchildbyidnew")
    @Operation(summary = "根据ID获得实时数据维护子表")
    public MyCommonResult<RtMaintainChildRespVO> getRtMaintainChildByCodeNew(@RequestParam("id")  Long id ) {
        RtMaintainChildDO rtMaintainChild = rtMaintainChildService.getRtMaintainChildById(id);
        Map<String, Object> result = new HashMap<>();
        if(rtMaintainChild != null ){
            List<Map<String, Object>> dataList = createDataList(rtMaintainChild);
            //构造成功输出结果
            result.put("code", 200);
            result.put("data", dataList);
            result.put("msg", "获取成功");
            return new MyCommonResult<>(result);
        }else {
            result.put("code", 404);
            result.put("data", "");
            result.put("msg", "对应数据找不到");
            return new MyCommonResult<>(result);
        }
    }
    @RequestMapping("/getchildbycodenew")
    @Operation(summary = "根据仪器编号获得最新实时数据维护子表")
    public MyCommonResult<RtMaintainChildRespVO> getRtMaintainChildByCodeNew(@RequestParam("code")  String code ) {
        RtMaintainChildDO rtMaintainChild = rtMaintainChildService.getRtMaintainChildByCode( code );
        Map<String, Object> result = new HashMap<>();
        if(rtMaintainChild != null ){
            List<Map<String, Object>> dataList = createDataList(rtMaintainChild);
            //构造成功输出结果
            result.put("code", 200);
            result.put("data", dataList);
            result.put("msg", "获取成功");
            return new MyCommonResult<>(result);
        }else {
            result.put("code", 404);
            result.put("data", "");
            result.put("msg", "对应数据找不到");
            return new MyCommonResult<>(result);
        }
    }
    @RequestMapping("/getchildbycreatetimenew")
    @Operation(summary = "根据时间区间，仪器代码获得实时数据维护子表")
    public MyCommonResult<List<Map<String, Object>>> getRtMaintainChildByCreateTimeNew(@Valid RtMaintainChildPageReqVO reqVO) {
        RtMaintainChildDO rtMaintainChild = rtMaintainChildService.getRtMaintainChildNewOne(reqVO);
        Map<String, Object> result = new HashMap<>();
        if(rtMaintainChild != null ){
            List<Map<String, Object>> dataList = createDataList(rtMaintainChild);
            //构造成功输出结果
            result.put("code", 200);
            result.put("data", dataList);
            result.put("msg", "获取成功");
            return new MyCommonResult<>(result);
        }else {
            result.put("code", 404);
            result.put("data", "");
            result.put("msg", "对应数据找不到");
            return new MyCommonResult<>(result);
        }
    }

    @RequestMapping("/getrtparameternewone")
    @Operation(summary = "根据井号与井ID要求每请求最新一条数据")
    public MyCommonResult<Map<String, Object>>getRTParameterNewOne(@Valid RtMaintainChildPageReqVO reqVO){
        RtMaintainChildDO rtMaintainChild = rtMaintainChildService.getRtMaintainChildNewOne(reqVO);
        Map<String, Object> result = new HashMap<>();
        if(rtMaintainChild != null ){
            List<Map<String, Object>> dataList = createDataList(rtMaintainChild);
            //构造成功输出结果
            result.put("code", 200);
            result.put("data", dataList);
            result.put("msg", "获取成功");
            return new MyCommonResult<>(result);
        }else {
            result.put("code", 404);
            result.put("data", "");
            result.put("msg", "对应数据找不到");
            return new MyCommonResult<>(result);
        }
    }



//---------------------------------------------------------------------------------------------------------------------------------------------




    /**
     * 创建符合格式包含属性名(name)和属性值(value)的Map对象。
     * @param name 属性名。
     * @param value 属性值。
     * @return 包含属性名和属性值的Map对象。
     */
    private Map<String, Object> createDataMap(String name, Object value) {
        Map<String, Object> dataMap = new HashMap<>();
        dataMap.put("name", name);
        dataMap.put("value", value);
        return dataMap;
    }

    /**
     * 创建包含属性名和属性值的Map对象列表。
     * @param rtMaintainChild 实时数据维护子表领域对象。
     * @return 包含属性名和属性值的Map对象列表。
     */
    private List<Map<String, Object>> createDataList(RtMaintainChildDO rtMaintainChild) {
        List<Map<String, Object>> dataList = new ArrayList<>();

        // 将RtMaintainChildDO对象的每个属性转换为Map对象，并添加到dataList中
        /*dataList.add(createDataMap("id", rtMaintainChild.getId()));
        dataList.add(createDataMap("parentId", rtMaintainChild.getParentId()));
        dataList.add(createDataMap("code", rtMaintainChild.getCode()));*/
        // 添加p1到p124属
        for (int i = 1; i <= 124; i++) {
            String methodName = "getP" + i;
            try {
                Method method = RtMaintainChildDO.class.getMethod(methodName);
                Object value = method.invoke(rtMaintainChild);
                dataList.add(createDataMap("p" + i, value));
            } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
                // 处理异常，例如记录日志或添加一个错误处理的Map对象
                e.printStackTrace();
                dataList.add(createDataMap("p" + i, null));
            }
        }
        /*dataList.add(createDataMap("state", rtMaintainChild.getState()));
        dataList.add(createDataMap("remark", rtMaintainChild.getRemark()));
        dataList.add(createDataMap("createTime", rtMaintainChild.getCreateTime()));*/
        return dataList;
    }


}